<?php
$link = mysql_connect('mysql-88.ukraine.com.ua', 'barthez_ai', 'x9gedv4w');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

$sql = "SELECT SUM(rating) AS count FROM imdb250";
mysql_select_db('barthez_ai');

$result = mysql_query($sql);

while($row = mysql_fetch_assoc($result)){
    $allVotesCount = $row['count'];
}

$sql = "SELECT * FROM `imdb250` ORDER BY RAND() LIMIT 3";
$result = mysql_query($sql);

$films = array();
while($row = mysql_fetch_array($result)){
    $films[$row['id']]=$row;
}



$sql = "SELECT * FROM `imdb250` ORDER BY rating DESC LIMIT 10";
$result = mysql_query($sql);

$topfilms = array();
while($row = mysql_fetch_array($result)){
    $topfilms[$row['id']]=$row;
}
    
?>
<script type="text/javascript" src="jquery.js"></script>

<style>
    body{
        font-family: verdana;
    }
    .main{
        width: 740px;
        border: 1px solid black;
        margin: 0px auto;
        background-color: #627AAD;
        padding: 5px;
        height: 660px;
    } 
    .oneFilm{
        width: 220px;
        display: inline-block;
        text-align: center;
    }
    
    .oneFilm .header{
        text-align: center;
        margin: 2px;
        padding: 2px;
        font-size: 11px;
        vertical-align: top;
    }
    
    .oneFilm .header a, .oneFilm .header a:hover, .oneFilm .header a:visited {
        color: white;
    }
    
    
    .films{
        width: 676px;
        margin: 0px auto;
    }
    
    .image{
        
        margin: 5px;
        padding: 5px;
        background-color: white;

    }
    .hide{
        display: none;
    }
    
    .footer{
        text-align: center;
        color: white;
        font-size: 16px;
    }
    
    .votes{
        font-size: 12px;
        color: #ddd;
        line-height: 12px;
    }
    .white{
        font-size: 12px;
        line-height: 14px;
        color: white;
        text-decoration:none;
    }
    .white:hover{
        text-decoration: underline;
    }
    
    .title{
        color: white;
        font-size: 16px;
        font-weight: bold;
        margin: 10px;
        padding: 10px;
    }
    
    .stLeft{
        
        vertical-align: top;
        width: 350px;
        display: inline-block;
        text-align: left;
        height: 190px;
        padding: 4px;
    }
    
    .skipThis{
        width: 300px;
        
        margin: 10px auto;
        
        
        padding: 5px;
        padding-top: 40px;
        
        text-align: center;
    }
    
    .skipThis a, .skipThis a:hover, .skipThis a:visited{
        color: white;
    }
    
    .minni{
        font-size: 8px;
        color: white;
    }
</style>
<?

//         $app_id = '105580809522325';
//
//         $canvas_page = 'http://deeflow.com/appKinoRating/';
//
//         $message = "test";
//
//         $feed_url = "http://www.facebook.com/dialog/feed?app_id=" 
//                . $app_id . "&redirect_uri=" . urlencode($canvas_page)
//                . "&message=" . $message;
//
//         if (empty($_REQUEST["post_id"])) {
//            echo("<script> top.location.href='" . $feed_url . "'</script>");
//         } else {
//            echo ("Feed Post Id: " . $_REQUEST["post_id"]);
//         }

         

?>
<head>
    
    <meta name="google-site-verification" content="fyuedQpG-mhkgjffEXP9LLDzJJONW3pZDdIrydWaoEQ" />
    <div id="fb-root"></div>
    <script src="http://connect.facebook.net/en_US/all.js"></script>
    <script>
        FB.init({
            appId  : '105580809522325',
            status : true, // check login status
            cookie : true, // enable cookies to allow the server to access the session
            xfbml  : true  // parse XFBML
        });
        
    </script>
    <script type="text/javascript">

                    var _gaq = _gaq || [];
                    _gaq.push(['_setAccount', 'UA-19921719-1']);
                    _gaq.push(['_trackPageview']);

                    (function() {
                        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
                        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
                        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
                    })();
    </script>
</head>

<div class="main">
    
    <div class="footer">
        <span class="title"> Please, select a movie, which like more than others by clicking on it. </span>
    </div>
    
    <div class="films">
        <? foreach($films as $film):?>
            <div class="oneFilm">
                <a href="javascript:void(0);" title="I Like <? print $film['name']?>">
                    <img id="<? print $film['id']?>" class="image" src="./images/<? print $film['id']?>.jpeg" alt="" title="<? print $film['name']?>"/>
                </a>
                <div class="header">
                    <a class="minni" title="Look '<? print $film['name']?>' on IMDB.com (open in new tab)" href="<?= $film['imdblink']?>" target="_blank">look this on imdb.com</a>
                </div>                
                    
                

            </div>    
        <? endforeach;?>
    </div>
    
    <div class="footer">
        <span id="text">Please, make your choice <a class="white" title="Show another films"  href="javascript:location.href='./'">or skip these films</a></span>
    </div>
    
    <div class="footer">
        
        <hr width="90%"/>
        
        
        <div class="stLeft">
            <div style="text-align: center; margin-bottom: 12px;">
                Statistics: TOP-10 voted <span class="votes">(total <? print $allVotesCount?> votes)</span>
            </div>
            <? $counter=1;?>
            <? foreach($topfilms as $film):?>
                <div>
                   <? print $counter?>
                    <a class="white" href="<?print $film['imdblink']?>" target="_blank"><? print $film['name']?>     </a>
                    <span class="votes">(<? print $film['rating']?> votes)</span>
                   <? $counter++?>
                </div>
            <? endforeach;?>
        </div>
        <div class="stLeft">
            <script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like href="http://apps.facebook.com/filmsrate/" show_faces="true" width="340" action="recommend" font="verdana"></fb:like>
            
            <div class="skipThis">
                <a href="javascript:location.href='./'">Skip (not to vote)</a>
            </div>
            
        </div>
    </div>
</div>

<script>
    $(document).ready(function(){
       
       var el = $("#text").html();
       $(".image").mouseenter(function(){
           $("#text").text("My choice: '" + $(this).attr('title')+"'");
       });
       
       $(".image").mouseleave(function(){
           $("#text").html(el);
       });
       
       $(".image").click(function(){
           $.ajax({
              url: "ajaxRatingChange.php?id="+$(this).attr('id'),
              success: function(){
                location.href = "./";
              }
            });
       });
       
       
       
    });
</script>